package com.evan.webadmin.biz.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.constant.BizConstant;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.util.MyModelUtil;
import com.evan.common.core.util.MyPageUtil;
import com.evan.webadmin.biz.dto.ExamPaperDto;
import com.evan.webadmin.biz.dto.ExamPaperFilter;
import com.evan.webadmin.biz.helper.DictHelper;
import com.evan.webadmin.biz.model.ExamPaper;
import com.evan.webadmin.biz.model.ExamPaperDetail;
import com.evan.webadmin.biz.service.IExamPaperDetailService;
import com.evan.webadmin.biz.service.IExamPaperService;
import com.evan.webadmin.biz.vo.ExamPaperVo;
import com.evan.webadmin.biz.vo.SubjectFrontVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 试卷表 前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2025-02-28
 */
@RestController
@RequestMapping("/admin/biz/exam/paper")
public class ExamPaperController {

    @Autowired
    private IExamPaperService examPaperService;
    @Autowired
    private IExamPaperDetailService examPaperDetailService;
    @Autowired
    DictHelper dictHelper;

    @PostMapping("/findById")
    public ResponseResult<ExamPaper> findById(@RequestBody ExamPaperFilter examPaperFilter) {
        return  ResponseResult.success(examPaperService.findById(examPaperFilter.getPaperId()));
    }

    @PostMapping("/findByPage")
    public ResponseResult<MyPageData<ExamPaperVo>> findPageByFront(@MyRequestBody ExamPaperFilter paperFilter,
                                                                 @MyRequestBody MyPageParam pageParam){
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<ExamPaper> list = examPaperService.findExamPaperByTypeAndName(paperFilter.getPersonnelType(), paperFilter.getModuleType(), paperFilter.getPaperName());
        long totalCount = 0L;
        if (list instanceof Page) {
            totalCount = ((Page<ExamPaper>) list).getTotal();
        }
        List<ExamPaperVo> paperVos = MyModelUtil.copyCollectionTo(list, ExamPaperVo.class);
        paperVos.forEach(i-> {
            i.setPersonnel(dictHelper.getDictMap(BizConstant.DICTTYPE_PERSONNEL_CODE).get(i.getPersonnelType() + ""));
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(paperVos, totalCount));
    }

    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody ExamPaperDto paper) {
        examPaperService.edit(paper);
        return ResponseResult.success();
    }

    @PostMapping("/delete/{paperId}")
    public ResponseResult delete(@PathVariable Long paperId) {
        examPaperService.deleteExamPaper(paperId);
        return ResponseResult.success();
    }

    @PostMapping("/refresh/{paperId}")
    public ResponseResult refresh(@PathVariable Long paperId) {
        examPaperService.refreshExamPaper(paperId);
        return ResponseResult.success();
    }

    @PostMapping("/detail")
    public ResponseResult<List<SubjectFrontVo>> detail(@MyRequestBody Long paperId) {
        List<ExamPaperDetail> details = examPaperDetailService.detail(paperId);
        List<SubjectFrontVo> subjectFrontVos =  details.stream().map(i -> {
            return JSONUtil.toBean(i.getSubjectContent(), SubjectFrontVo.class);
        }).collect(Collectors.toList());
        return ResponseResult.success(subjectFrontVos);
    }

    @PostMapping("/publish")
    public ResponseResult publish(@MyRequestBody Long paperId) {
        examPaperService.publish(paperId);
        return ResponseResult.success();
    }

    @PostMapping("/publish/get")
    public ResponseResult<ExamPaper> publishGet(@MyRequestBody Integer personnelType,@MyRequestBody Integer moduleType) {
        return ResponseResult.success(examPaperService.findPublishedPaper(personnelType, moduleType));
    }

    @PostMapping("/list")
    public ResponseResult<List<ExamPaper>> list(@MyRequestBody ExamPaperFilter examPaperFilter) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return ResponseResult.success(examPaperService.list(queryWrapper));
    }

    @PostMapping("/logic/delete")
    public ResponseResult logicDelete(@MyRequestBody Long paperId) {
        examPaperService.logicDelete(paperId);
        return ResponseResult.success();
    }
}

